#!/usr/bin/env bash
# Copyright (c) 2023 The Toltec Contributors
# SPDX-License-Identifier: MIT

pkgnames=(zerotier zerotier-one zerotier-one-doc zerotier-selftest)
url=https://github.com/zerotier/ZeroTierOne
_upver=1.14.1
pkgver=${_upver}-1
timestamp=2023-03-23T17:39:31Z
maintainer="Eeems <eeems@eeems.email>"
license=BUSL-1.1
section="utils"
image=base:v3.0

source=("https://github.com/zerotier/ZeroTierOne/archive/refs/tags/${_upver}.zip")
sha256sums=(fcf30ce797dbca757f3e882e004a3f953ebe12ae13f03d804b3fb184ee3c9714)

build() {
    sed -i \
        -e 's|/usr/sbin|/opt/bin|' \
        -e 's|/usr/share|/opt/share|' \
        -e 's|/var/lib|/opt/var/lib|' \
        make-linux.mk \
        debian/zerotier-one.service
    sed -i \
        -e 's|/var/lib/zerotier-one|/opt/var/lib/zerotier-one|' \
        osdep/OSUtils.cpp
    sed -i \
        -e 's|/var/lib/zerotier|/opt/var/lib/zerotier|' \
        doc/zerotier-cli.1 \
        doc/zerotier-cli.1.md \
        doc/zerotier-one.8 \
        doc/zerotier-one.8.md
    # Disable SSO as the rust build is broken in rust:v3.0 due to issues with dependencies
    export ZT_SSO_SUPPORTED=0
    export ZT_EMBEDDED=1
    export CC=arm-linux-gnueabihf-gcc
    export CXX=arm-linux-gnueabihf-g++
    export AR=arm-linux-gnueabihf-ar
    export STRIP=arm-linux-gnueabihf-strip
    make
    make selftest
}

zerotier() {
    pkgdesc="A Smart Ethernet Switch for Earth"
    installdepends=(zerotier-one)
    # Using 1: to make sure our version always overrides the entware version
    pkgver="1:${pkgver}"
    package() {
        # This is just a wrapper package
        true
    }
}

zerotier-one() {
    pkgdesc="A Smart Ethernet Switch for Earth"
    package() {
        cd "$srcdir"
        DESTDIR="$pkgdir" make install
        rm -r "$pkgdir"/opt/share
        install -D -m0644 debian/zerotier-one.service "$pkgdir"/etc/systemd/system/zerotier-one.service
    }
    configure() {
        if ! is-enabled zerotier-one.service; then
            how-to-enable zerotier-one.service
        fi
        if is-active zerotier-one.service; then
            systemctl restart zerotier-one.service
        fi
    }
    preremove() {
        disable-unit zerotier-one.service
    }
}

zerotier-one-doc() {
    installdepends=(man-db)
    pkgdesc="Man files for zerotier-one"
    package() {
        cd "$srcdir"
        DESTDIR="$pkgdir" make install
        rm -r "$pkgdir"/opt/bin
        rm -r "$pkgdir"/opt/var
    }
}

zerotier-selftest() {
    pkgdesc="Unit test zerotier to ensure that it works as expected on the system"
    package() {
        install -D -m 755 -t "$pkgdir"/opt/bin "$srcdir"/zerotier-selftest
    }
}
